package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SyncEvent;
	import flash.net.NetConnection;
	import flash.net.SharedObject;

	import fla.view.Island;

	//import flash.net.ObjectEncoding;

	public class MinSOmc extends Sprite
	{
		private var pointer_so:SharedObject;

		private var nc:NetConnection;

		private var good:Boolean;

		private var island:Island;

		public function MinSOmc()
		{
			//NetConnection.defaultObjectEncoding=flash.net.ObjectEncoding.AMF0;
			//Connect
			var rtmpNow:String = "rtmp://192.168.1.209/test";
			nc = new NetConnection;
			nc.connect(rtmpNow);
			nc.addEventListener(NetStatusEvent.NET_STATUS, doSO);
			island = new Island;
			addChild(island);
			island.x = 200;
			island.y = 200;
			island.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
			island.addEventListener(MouseEvent.MOUSE_UP, endDrag);
		}

		private function doSO(e:NetStatusEvent):void
		{
			good = e.info.code == "NetConnection.Connect.Success";
			if (good)
			{
				//Shared object
				pointer_so = SharedObject.getRemote("point", nc.uri, false);
				pointer_so.connect(nc);
				pointer_so.addEventListener(SyncEvent.SYNC, doUpdate);
			}
		}

		private function doUpdate(se:SyncEvent):void
		{
			for (var cl:uint; cl < se.changeList.length; cl++)
			{
				trace(se.changeList[cl].code);
				if (se.changeList[cl].code == "change")
				{
					switch (se.changeList[cl].name)
					{
						case "xpos":
							island.x = pointer_so.data.xpos;
							break;
						case "ypos":
							island.y = pointer_so.data.ypos;
							break;
					}
				}
			}
		}

		private function beginDrag(e:MouseEvent):void
		{
			island.addEventListener(MouseEvent.MOUSE_MOVE, moveMc);
			island.startDrag();
		}

		private function endDrag(e:MouseEvent):void
		{
			island.stopDrag();
		}

		private function moveMc(e:MouseEvent):void
		{
			e.updateAfterEvent();
			pointer_so.setProperty("xpos", island.x);
			pointer_so.setProperty("ypos", island.y);
		}
	}
}
